package com.example.charlotte.snakegame;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;

public class SettingActivity extends BaseActivity implements View.OnClickListener {
    private SharedPreferences saved;
    private SharedPreferences.Editor editor;
    private RadioButton radioButton1,radioButton2;
    private CheckBox checkBox1,checkBox2;
    private GameMusic music;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        init();
        music=new GameMusic(this);
        saved = PreferenceManager.getDefaultSharedPreferences(this);
        checkBox1.setChecked(saved.getBoolean("bgmusic", true));
        checkBox2.setChecked(saved.getBoolean("soundmusic", true));
        if (saved.getBoolean("gesture", true)){
            radioButton1.setChecked(true);
        }else {
            radioButton2.setChecked(true);
        }

    }

    private void init() {
        radioButton1=findViewById(R.id.radio1);
        radioButton2=findViewById(R.id.radio2);
        checkBox1=findViewById(R.id.checkBox1);
        checkBox2=findViewById(R.id.checkBox2);
        radioButton1.setOnClickListener(this);
        radioButton2.setOnClickListener(this);
        checkBox1.setOnClickListener(this);
        checkBox2.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        editor = saved.edit();
        switch (view.getId()) {
            case R.id.radio1:
                radioButton1.setChecked(true);
                editor.putBoolean("gesture", true);
                break;
            case R.id.radio2:
                radioButton2.setChecked(true);
                editor.putBoolean("gesture", false);
                break;
            case R.id.checkBox1:
                if (checkBox1.isChecked()) {
                    editor.putBoolean("bgmusic", true);
                    Intent intent = new Intent(SettingActivity.this, BGMService.class);
                    startService(intent);
                } else {
                    editor.putBoolean("bgmusic", false);
                    Intent intent = new Intent(SettingActivity.this, BGMService.class);
                    stopService(intent);
                }
                break;
            case R.id.checkBox2:
                if (checkBox1.isChecked()) {
                    editor.putBoolean("soundmusic", true);

                }else {
                    editor.putBoolean("soundmusic", false);

                }
                break;
        }
        music.startMusic(3);
        editor.commit();
    }
}
